package com.example.lanouhn.firstapp.activity.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.lanouhn.firstapp.R;
import com.example.lanouhn.firstapp.activity.service.Tools;
import com.example.lanouhn.firstapp.activity.view.CityPickerDialog;

/**
 * 添加地址页面
 */
public class AddAddressActivity extends AppCompatActivity {

    //返回
    private ImageView iv_back;
    //选择省市
    private EditText main_province;
    //在新地址里面添加姓名、电话等
    private EditText et_name;
    private EditText et_phone;
    private EditText et_phoneNumber;
    private TextView et_province;
    private EditText et_country;
    private EditText et_street;
    //保存地址
    private TextView tv_addAddress;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_address);
        iv_back = (ImageView) findViewById(R.id.iv_back);

        tv_addAddress = (TextView) findViewById(R.id.tv_addAddress);
        et_name = (EditText) findViewById(R.id.et_name);
        et_phone = (EditText) findViewById(R.id.et_phone);
        et_phoneNumber = (EditText) findViewById(R.id.et_phoneNumber);
        et_province = (TextView) findViewById(R.id.et_province);
        et_country = (EditText) findViewById(R.id.et_country);
        et_street = (EditText) findViewById(R.id.et_street);



        //返回监听事件
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        //选择省市监听事件
        et_province.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (!Tools.isFastDoubleClick()) {

                    new CityPickerDialog(AddAddressActivity.this,
                            new CityPickerDialog.OnCityPikerListener() {

                                @Override
                                public void onCityPicker(String province,
                                                         String city) {
                                    et_province.setText(province + "  " + city);

                                }
                            }
                    ).show();
                }
            }
        });


        tv_addAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();

                if (!et_name.getText().toString().trim().equals("")
                        && !et_phone.getText().toString().trim().equals("")
                        && !et_province.getText().toString().trim().equals("")
                        && !et_country.getText().toString().trim().equals("")
                        && !et_street.getText().toString().trim().equals("")
                        ) {


                    intent.putExtra("name", et_name.getText().toString().trim());
                    intent.putExtra("phone", et_phone.getText().toString().trim());
                    intent.putExtra("province", et_province.getText().toString().trim());
                    intent.putExtra("country", et_country.getText().toString().trim());
                    intent.putExtra("street", et_street.getText().toString().trim());
                    setResult(102, intent);
                    finish();
                }

            }
        });

    }
}
